define(['../../common/common', '../../common/pathCompute', 'ztree'], function($, pathfn, ztree) {
	var interface =  pathfn.interface;
  	projectDw = function(projectId){
  		layui.use([ 'table','form','laypage','layedit' ],function() {			
          	var table = layui.table,
          	form = layui.form,
          	layedit = layui.layedit,
          	laypage = layui.laypage,
          	init= function(){
          		ztreeOperatea.ztreeInit();
            	bindEvents();
          	},
          	ztreeOperatea = {
				setting: {
					view: {
			            showLine: false,
			            showIcon: false,
			            addDiyDom: function(treeId, treeNode){
							return ztreeOperatea.addDiyDom(treeId, treeNode);
					    }
			        },
			        data: {
			            simpleData: {
			                enable: true
			            },
			            key: {
							name: "code"
						}
			        },
					async: {
						enable: true,
						url: interface.projectItemInfo,
						otherParam: ["bitProjectId", projectId],
						dataFilter: function(treeId, parentNode, responseData){
							return ztreeOperatea.ajaxDataFilter(treeId, parentNode, responseData);
					    }
					},
					callback: {
						onAsyncSuccess: function(event, treeId, treeNode, msg) {
							//添加表头
					        var li_head = ' <li class="head"><a><div class="diy">编码</div><div class="diy">材料换算系数</div><div class="diy">运输换算系数</div>' +
					            '<div class="diy">安装还俗系数</div><div class="diy">类别</div><div class="diy">名称</div><div class="diy">项目特征</div>'+
					            '<div class="diy">单位</div><div class="diy">工程量</div><div class="diy">不含税单价</div><div class="diy">含税单价</div>'+
					            '<div class="diy">不含税合价</div><div class="diy">含税合价</div></a></li>';
					        var rows = $("#dataTree").find('li');
					        
					        if (rows.length > 0) {
					            rows.eq(0).before(li_head)
					        } else {
					            $("#dataTree").append(li_head);
					            $("#dataTree").append('<li ><div style="text-align: center;line-height: 30px;" >无符合条件数据</div></li>')
					        }
						},
						onClick: function(even, treeId, treeNode) {
						}
					}
				},
				ztreeInit: function() { //加载左侧菜单树
					$.fn.zTree.init($("#dataTree"), this.setting);
				},
				addDiyDom: function(treeId, treeNode){
					console.log(treeNode)
			        var _this = this;
			        var spaceWidth = 15;
			        var liObj = $("#" + treeNode.tId);
			        var aObj = $("#" + treeNode.tId + "_a");
			        var switchObj = $("#" + treeNode.tId + "_switch");
			        var icoObj = $("#" + treeNode.tId + "_ico");
			        var spanObj = $("#" + treeNode.tId + "_span");
			        aObj.attr('title', '');
			        aObj.append('<div class="diy swich"></div>');
			        var div = $(liObj).find('div').eq(0);
			        console.log(div)
			        switchObj.remove();
			        spanObj.remove();
			        icoObj.remove();
			        div.append(switchObj);
			        div.append(spanObj);
			        var spaceStr = "<span style='height:1px;display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
			        switchObj.before(spaceStr);
			        console.log(switchObj.find('.node_name'))
			        var editStr = '';			        
			        editStr += '<div class="diy">' + (!treeNode.matesRate && treeNode.matesRate !== 0 ? '&nbsp;' : treeNode.matesRate ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.transRate && treeNode.transRate !== 0 ? '&nbsp;' : treeNode.transRate ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.instsRate && treeNode.instsRate !== 0 ? '&nbsp;' : treeNode.instsRate ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.Type && treeNode.Type !== 0 ? '&nbsp;' : treeNode.Type ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.name && treeNode.name !== 0 ? '&nbsp;' : treeNode.name ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.projectProp && treeNode.projectProp !== 0 ? '&nbsp;' : treeNode.projectProp ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.unit && treeNode.unit !== 0 ? '&nbsp;' : treeNode.unit ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.dtgcl && treeNode.dtgcl !== 0 ? '&nbsp;' : treeNode.dtgcl ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.singlePrice && treeNode.singlePrice !== 0 ? '&nbsp;' : treeNode.singlePrice ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.price && treeNode.price !== 0 ? '&nbsp;' : treeNode.price ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.taxSinglePrice && treeNode.taxSinglePrice !== 0 ? '&nbsp;' : treeNode.taxSinglePrice ) + '</div>';
			        editStr += '<div class="diy">' + (!treeNode.taxSingleSumPrice && treeNode.taxSingleSumPrice !== 0 ? '&nbsp;' : treeNode.taxSingleSumPrice ) + '</div>';
			        editStr += '<div class="diy">' + _this.formatHandle(treeNode) + '</div>';
			        aObj.append(editStr);
			    },
			    ajaxDataFilter: function(treeId, parentNode, responseData) {
			    	var child = responseData.children;
				    if (child.length) {
				      for(var i =0; i < child.length; i++) {
				        child[i].code = child[i].code ? child[i].code : child[i].name;
				        child[i].open = !child[i].leaf;
				      }
				    }
				    return child;
				},
			    formatHandle: function(treeNode) {
			        var htmlStr = '';
			        htmlStr += '<div class="icon_div"><a class="icon_view" title="查看" onclick="view(\'' + treeNode.id + '\')"  href="javascript:;">查看</a></div>';
			        htmlStr += '<div class="icon_div"><a class="icon_edit" title="修改" href="javascript:edit(\'' + treeNode.id + '\')">修改</a></div>';
			        htmlStr += '<div class="icon_div"><a class="icon_add" title="添加子部门" onclick="addSector(\'' + treeNode.id + '\')" href="javascript:;">添加子部门</a></div>';
			        htmlStr += '<div class="icon_div"><a class="icon_del" title="删除" href="javascript:del(\'' + treeNode.tId + '\')">删除</a></div>';
			        return htmlStr;
			    }
	        },      	
          	projectIofo= {
          		tableInitObj: function(){
					var listObj = {
						P_BASE: {
							id: 'projectInfo-base',
			                elem: '#projectInfo-base', //指定原始表格元素选择器（推荐id选择器）
			                height: 'full-140', //容器高度
			                cols:  [[ //标题栏
			                  {field:'no', title: '序号', width:100, sort: true, align:'center'}
			                  ,{field:'name', title: '名称', width:280, sort: true}
			                  ,{field:'value', title: '内容', width:280}
			                ]],
			                url: interface.projectInfo,
			                method: 'get',
			                page: false, //开启分页
			                initSort: {
			                  field: 'name' //排序字段，对应 cols 设定的各字段名
			                  ,type: 'desc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
			                },
			                loading: true
						},
						P_OTHER: {
							id: 'projectInfo-other',
			                elem: '#projectInfo-other', //指定原始表格元素选择器（推荐id选择器）
			                height: 'full-180', //容器高度
			                cols:  [[ //标题栏
			                  {field:'no', title: '序号', width:100, sort: true, align:'center'}
			                  ,{field:'name', title: '名称', width:280, sort: true}
			                  ,{field:'value', title: '内容', width:280}
			                  ,{field:'remark', title: '备注', width:280}
			                  ,{field:'caozuo', fixed: 'right', title: '操作', width:160, align:'center', toolbar: '#barDemo'}
			                ]],
			                url: interface.projectInfo,
			                method: 'get',
			                page: false, //开启分页
			                initSort: {
			                  field: 'name' //排序字段，对应 cols 设定的各字段名
			                  ,type: 'desc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
			                },
			                loading: true
						},
						P_COST: {
							id: 'projectInfo-cost',
			                elem: '#projectInfo-cost', //指定原始表格元素选择器（推荐id选择器）
			                height: 'full-140', //容器高度
			                cols:  [[ //标题栏
			                  {field:'no', title: '序号', width:100, sort: true, align:'center'}
			                  ,{field:'name', title: '名称', width:280, sort: true}
			                  ,{field:'value', title: '内容', width:280}
			                ]],
			                url: interface.projectInfo,
			                method: 'get',
			                page: false, //开启分页
			                initSort: {
			                  field: 'name' //排序字段，对应 cols 设定的各字段名
			                  ,type: 'desc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
			                },
			                loading: true
						}

					}	              			
	              	return listObj;           
	            },
	            tableInit: function(info){
		            var _this = this,
		            defaults = {
		                where: {
		                  projectId: projectId,
		                  type: info
		                },
		                done: function(res, curr, count){
		                }
		            }
		            console.log(_this.tableInitObj()[info])
		            $.extend(true, defaults, _this.tableInitObj()[info]);        
		            table.render(defaults);
		        }
          	},
          	bindEvents= function(){
          		table.on('tool(projectInfoOtherEvent)', function(obj){
	                var dataObj = obj.data,
	                delContent = '确认删除名称为'+dataObj.name+'的一行吗？';
	                if(obj.event === 'del'){
	                	console.log(delContent)
	                  var openObj = {
	                    "content": delContent,
	                    "title":'删除',
	                    "success": function(layero, index){
	                      
	                    },
	                    "yes": function(layero, index){
	                      
	                        layer.close(index);
	                        
	                    }
	                  }
	                  layerOpenfn(openObj);
	                }
	            });
          		$('.projectInfo').each(function(index, el) {
      				var $li = $(this).find('.projectInfo-title').find('li'),
      				$list = $(this).find('.project-content').find('.project-list');
      				$li.each(function(index, el) {          			
          				$(this).click(function(){
          					var info = $(this).data('info');
          					$li.removeClass('active').eq(index).addClass('active');
	          				$list.hide(0).eq(index).show(0);
          					if(info == 'P_CODE'){
          						$.ajaxRequest({
							    	url: interface.projectInfo,
							    	data:{
										projectId: projectId,
		                  				type: info
									},
							    	successfn: function(data) {	
							    		$('#projectInfo-code').html(data.data[0] && data.data[0].codeInfo);
							    		layedit.build('projectInfo-code',{
		          							height:'full-200',
		          							tool: ['strong','italic','underline','del','|' ,'left','center','right','link','unlink','face']
		          						});
							    	}
							    })
          					}else if(info == 'P_COSTER'){

          					}else{	          					
	          					projectIofo.tableInit(info);
          					}
          					
          				})
          			});
      			});
          	};
          	init();
        })
	}
	return projectDw;
})
